## https://tianchi.aliyun.com/notebook-ai/detail
## 模拟练习
##  基础函数库
## 绘图函数库
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
## 导入LightGBM模型
from lightgbm.sklearn import LGBMClassifier
from sklearn import metrics
from sklearn.model_selection import GridSearchCV
## 为了正确评估模型性能，将数据划分为训练集和测试集，并在训练集上训练模型，在测试集上验证模型性能。
from sklearn.model_selection import train_test_split

## 我们利用Pandas自带的read_csv函数读取并转化为DataFrame格式
df = pd.read_csv('../../doc/promotion/aicampml/happiness_train_abbr.csv')
y = df.happiness
x = df.drop(["happiness","survey_time"],axis=1)
## 利用.info()查看数据的整体信息
# df.info()
# print(df.head)
# print(y.value_counts())
# print(df.describe())


## 为了正确评估模型性能，将数据划分为训练集和测试集，并在训练集上训练模型，在测试集上验证模型性能。
# from sklearn.model_selection import train_test_split
## 选择其类别为0和1的样本 （不包括类别为2的样本）
data_target_part = y
data_features_part = x
## 测试集大小为20%， 80%/20%分
x_train, x_test, y_train, y_test = train_test_split(data_features_part, data_target_part, test_size = 0.2, random_state = 2020)
## 导入LightGBM模型
# from lightgbm.sklearn import LGBMClassifier
## 定义 LightGBM 模型
clf = LGBMClassifier()
# 在训练集上训练LightGBM模型
clf.fit(x_train, y_train)


## 在训练集和测试集上分布利用训练好的模型进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)
# from sklearn import metrics
## 利用accuracy（准确度）【预测正确的样本数目占总预测样本数目的比例】评估模型效果
print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict))
print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict))
# 查看混淆矩阵 (预测值和真实值的各类情况统计矩阵)
confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)
print('The confusion matrix result:\n',confusion_matrix_result)


# ## 查看混淆矩阵 (预测值和真实值的各类情况统计矩阵)
# confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)
# print('The confusion matrix result:\n',confusion_matrix_result)
# # 利用热力图对于结果进行可视化
# plt.figure(figsize=(8, 6))
# sns.heatmap(confusion_matrix_result, annot=True, cmap='Blues')
# plt.xlabel('Predicted labels')
# plt.ylabel('True labels')
# plt.show()


# ## 从sklearn库中导入网格调参函数
# from sklearn.model_selection import GridSearchCV
# ## 定义参数取值范围
# learning_rate = [0.1, 0.3, 0.6]
# feature_fraction = [0.5, 0.8, 1]
# num_leaves = [16, 32, 64]
# max_depth = [-1,3,5,8]
# parameters = { 'learning_rate': learning_rate,
#               'feature_fraction':feature_fraction,
#               'num_leaves': num_leaves,
#               'max_depth': max_depth}
# model = LGBMClassifier(n_estimators = 50)
# ## 进行网格搜索
# clf = GridSearchCV(model, parameters, cv=3, scoring='accuracy',verbose=3, n_jobs=-1)
# clf = clf.fit(x_train, y_train)
# print(clf.best_params_)


## 在训练集和测试集上分布利用最好的模型参数进行预测
## 定义带参数的 LightGBM模型
# clf = LGBMClassifier(feature_fraction = 0.8,
#                     learning_rate = 0.1,
#                     max_depth= 8,
#                     num_leaves = 16)
# clf.fit(x_train, y_train)
# train_predict = clf.predict(x_train)
# test_predict = clf.predict(x_test)
# ## 利用accuracy（准确度）【预测正确的样本数目占总预测样本数目的比例】评估模型效果
# from sklearn import metrics
# print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict))
# print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict))
# The accuracy of the Logistic Regression is: 0.80890625
# The accuracy of the Logistic Regression is: 0.63125

if __name__ == '__main__':
    pass